package hfxy.dzx.tx.main;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class TestMap {
	public static void main(String []args) {
		Map<String,String> map=new HashMap<>();
		
		//增加数据
		map.put("id", "a3401");
		map.put("name", "liyi");
		map.put("age", "b51");

		
		//遍历
		System.out.println("直接打印map"+map);
		
		//1 键名集合
		for(String key:map.keySet()) {
			System.out.println("key is:"+key+"  value is:"+map.get(key));
		}
		//2项集合
		for(Map.Entry<String, String> entry:map.entrySet()) {
			System.out.println("key is:"+entry.getKey()+"  value is:"+entry.getValue());
		}
		
		//3项集合+迭代器
		Set set=map.entrySet();
		Iterator it=set.iterator();
		while(it.hasNext()) {
			Map.Entry<String, String> entry=(Entry<String, String>) it.next();
			System.out.println("key is:"+entry.getKey()+"  value is:"+entry.getValue());
		}
		
		//排序,hashmap排序借助list
		ArrayList<Map.Entry<String, String>> list=new ArrayList<Map.Entry<String, String>>(set);
		//无法使用内比较器
		//Collections.sort(list);//必须定义Comparable内比较器，与自己比，故一个参数
		//System.out.println("内比较器"+list);
		
		Collections.sort(list,new OutComp3());
		System.out.println("外比较器"+list);
		
		
		
		//treemap的排序之一，对键名是字符串可以比较
		Map<Object,Object> treemap1=new TreeMap<Object,Object>(new Comparator<Object>() {

			@Override
			public int compare(Object o1, Object o2) {
				// TODO Auto-generated method stub
				if(o1 instanceof String ) {
					return ((String)o1).compareTo(  ((String)o2)   );
				}
				else if(o1 instanceof Long) {
					return ((Long)o1).compareTo(  ((Long)o2)   );					
				}				
				return 0;
			}
		});	
		
		//增加数据
		treemap1.put("id", "a3401");
		treemap1.put("name", "liyi");
		treemap1.put("age", "b51");
		
		System.out.println("可以对键名字符串进行比较 Treemap1 is:"+treemap1);//字母比较
		
		//treemap的排序之二，对键名是数字可以比较
		Map<Object,Object> treemap2=new TreeMap<Object,Object>(new Comparator<Object>() {

			@Override
			public int compare(Object o1, Object o2) {
				// TODO Auto-generated method stub
				if(o1 instanceof String ) {
					//System.out.println("键名是字符串");
					return ((String)o1).compareTo(  ((String)o2)   );
				}
				else if(o1 instanceof Long) {
					//System.out.println("键名是数字");
					return ((Long)o1).compareTo(  ((Long)o2)   );					
				}				
				return 0;
			}
		});	
		
		//增加数据
		treemap2.put(Long.valueOf(2), "a3401");
		treemap2.put(Long.valueOf(11), "liyi");
		treemap2.put(Long.valueOf(13), "b51");
		
		System.out.println("可以对键名数字进行比较，Treemap2 is:"+treemap2);//

	}

}

class OutComp3 implements Comparator{
	@Override
	public int compare(Object o1, Object o2) {		
		//按键名进行排序，让某些字段在前，某些字段在后		
		String id1=((Entry<String, String>)o1).getKey();
		String id2=((Entry<String, String>)o2).getKey();		
		return id1.compareTo(id2)*-1;		
		
		//按键值进行排序也是可以的，但其实没有大的意义，试想，它们不是同字段，没有比较价值	
		/*String id1=((Entry<String, String>)o1).getValue();
		String id2=((Entry<String, String>)o2).getValue();		
		return id1.compareTo(id2);*/	
	}	
}
